package com.weixing.mall.provider.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.utils.EnumUtil;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.provider.api.model.enums.AgentLevelEnum;
import com.weixing.mall.provider.api.model.enums.ProfitFromEnum;
import com.weixing.mall.provider.mapper.AgentCommissionMapper;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.domain.AgentCommission;
import com.weixing.mall.provider.model.query.AgentCommissionQuery;
import com.weixing.mall.provider.model.vo.AgentCommissionVo;
import com.weixing.mall.provider.service.IAgentCommissionService;
import com.weixing.mall.provider.service.IAgentService;
import com.weixing.mall.util.number.NumberPatternEnum;
import com.weixing.mall.util.number.NumberToRmbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName AgentCommissionServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/10/15
 * @Version 1.0
 **/
/**
 * <p>
 * 代理商模块-代理商佣金表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentCommissionServiceImpl extends ServiceImpl<AgentCommissionMapper, AgentCommission> implements IAgentCommissionService {
    @Autowired
    private IAgentService agentService;

    @Override
    public IPage<AgentCommissionVo> selectListOfPage(Page page, AgentCommissionQuery agentCommissionQuery) {
        IPage<AgentCommissionVo> iPage = baseMapper.selectListOfPage(page, agentCommissionQuery);
        List<AgentCommissionVo> records = iPage.getRecords();
        if (CollUtil.isNotEmpty(records)){
            for (AgentCommissionVo vo : records){
                vo.setFmtCommissionAmount(NumberToRmbUtil.format(vo.getCommissionAmount(), NumberPatternEnum.PAT_DEFAULT.getValue()));
                vo.setFmtOrderPrice(NumberToRmbUtil.format(vo.getOrderPrice(), NumberPatternEnum.PAT_DEFAULT.getValue()));
            }
        }
        return iPage;
    }

    @Override
    public AgentCommissionVo commisionDetail(long id) {
        AgentCommission agentCommission = getById(id);
        Agent agent = agentService.getById(agentCommission.getAgentId());
        AgentCommissionVo agentCommissionVo = new AgentCommissionVo();
        agentCommissionVo.setId(agentCommission.getId());
        agentCommissionVo.setAgentId(agentCommission.getAgentId());
        agentCommissionVo.setAgentLevel(EnumUtil.getIEnum(AgentLevelEnum.class,agent.getAgentLevel()));
        agentCommissionVo.setAgentName(agent.getAgentName());
        agentCommissionVo.setCommissionAmount(agentCommission.getCommissionAmount());
        agentCommissionVo.setCustomerName(agentCommission.getKefu());
        agentCommissionVo.setIsEnable(EnumUtil.getIEnum(IsEnableEnum.class,agentCommission.getIsEnable()));
        agentCommissionVo.setCommissionFrom(EnumUtil.getIEnum(ProfitFromEnum.class,agentCommission.getCommissionFrom()));
        agentCommissionVo.setMobile(agent.getMobile());
        agentCommissionVo.setOrderId(agentCommission.getOrderId());
        agentCommissionVo.setOrderPrice(agentCommission.getOrderPrice());
        agentCommissionVo.setOrderSn(agentCommission.getOrderSn());
        agentCommissionVo.setOrderGmtTime(agentCommission.getOrderGmtTime());
        agentCommissionVo.setOrderAuditTime(agentCommission.getOrderAuditTime());
        return agentCommissionVo;
    }

    @Override
    public IPage<AgentCommissionVo> selectAgentCommsOfPage(Page initMpPage, long agentId) {
        return baseMapper.selectAgentCommsOfPage(initMpPage,agentId);
    }

    @Override
    public BigDecimal countComm(long agentId, String month) {
        return baseMapper.countComm(agentId,month);
    }

    @Override
    public AgentCommission findByOrderIdAndAgentId(Long orderId, Long agentId) {
        QueryWrapper<AgentCommission> wrapper = new QueryWrapper<AgentCommission>();
        wrapper.eq("order_id",orderId).eq("agent_id",agentId);
        return getOne(wrapper);
    }

}
